package com.zyw.zhuangdian.controller;

import com.alibaba.fastjson.JSONObject;
import com.zyw.zhuangdian.enums.MsgCode;
import com.zyw.zhuangdian.pojo.Resp;
import com.zyw.zhuangdian.pojo.in.MessageDtoIn;
import com.zyw.zhuangdian.pojo.out.MessageDtoOut;
import com.zyw.zhuangdian.service.MessageService;
import com.zyw.zhuangdian.util.Assert;
import com.zyw.zhuangdian.util.BeanConvertUtil;
import com.zyw.zhuangdian.util.RespUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/message")
@RequiredArgsConstructor
@Slf4j
public class MessageController {
    private final MessageService messageService;

    /**
     * 新增消息
     *
     * @param json
     * @return
     */
    @RequestMapping(value = "addMessage", method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> addMessage(@RequestBody JSONObject json) {
        Assert.hasKeyAndValue(json, "title", "消息标题不能为空");
        Assert.hasKeyAndValue(json, "fromUid", "发布人不能为空");
        Assert.hasKeyAndValue(json, "toUid", "接受人不能为空");
        Assert.hasKeyAndValue(json, "content", "消息内容不能为空");

        MessageDtoIn in = BeanConvertUtil.covertBean(json, MessageDtoIn.class);
        Resp<Void> resp = messageService.addMessage(in);
        return RespUtil.OK(resp);
    }

    /**
     * 修改消息
     *
     * @param json
     * @return
     */
    @RequestMapping(value = "updateMessage", method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> updateMessage(@RequestBody JSONObject json) {
        Assert.hasKeyAndValue(json, "mid", "主键ID不能为空");

        MessageDtoIn in = BeanConvertUtil.covertBean(json, MessageDtoIn.class);
        Resp<Void> resp = messageService.updateMessage(in);
        return RespUtil.OK(resp);
    }

    /**
     * 删除消息
     *
     * @param json
     * @return
     */
    @RequestMapping(value = "deleteMessage", method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> deleteMessage(@RequestBody JSONObject json) {
        Assert.hasKeyAndValue(json, "mid", "主键ID不能为空");

        MessageDtoIn in = BeanConvertUtil.covertBean(json, MessageDtoIn.class);
        Resp<Void> resp = messageService.deleteMessage(in);
        return RespUtil.OK(resp);
    }

    /**
     * 列出消息
     *
     * @param json
     * @return
     */
    @RequestMapping(value = "listMessages", method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> listMessages(@RequestBody JSONObject json) {

        MessageDtoIn in = BeanConvertUtil.covertBean(json, MessageDtoIn.class);
        Resp<List<MessageDtoOut>> resp = messageService.listMessages(in);
        return RespUtil.OK(resp);
    }

    /**
     * 列出未读消息数
     *
     * @param json
     * @return
     */
    @RequestMapping(value = "listUnreadMessageCount", method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> listUnreadMessageCount(@RequestBody JSONObject json) {
        Assert.hasKeyAndValue(json, "toUid", "接受人不能为空");

        MessageDtoIn in = BeanConvertUtil.covertBean(json, MessageDtoIn.class);
        Resp<Integer> resp = messageService.listUnreadMessageCount(in);
        return RespUtil.OK(resp);
    }

    /**
     * 标记消息已读
     *
     * @param json
     * @return
     */
    @RequestMapping(value = "readMessage", method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> readMessage(@RequestBody JSONObject json) {
        Assert.hasKeyAndValue(json, "receiverUid", "接受人不能为空");
        Assert.hasKeyAndValue(json, "midList", "消息ID不能为空");

        MessageDtoIn in = BeanConvertUtil.covertBean(json, MessageDtoIn.class);
        Resp<Void> resp = messageService.readMessages(in);
        return RespUtil.OK(resp);
    }

    /**
     * 标记消息已读
     *
     * @param json
     * @return
     */
    @RequestMapping(value = "readAllMessage", method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> readAllMessage(@RequestBody JSONObject json) {
        Assert.hasKeyAndValue(json, "toUid", "接受人不能为空");

        MessageDtoIn in = BeanConvertUtil.covertBean(json, MessageDtoIn.class);
        // 查出所有未读消息 并标记为已读
        Resp<List<MessageDtoOut>> resp = messageService.listUnreadMessages(in);
        if (resp.hasData()) {
            List<MessageDtoOut> dtoOuts = resp.getData();
            List<Long> mids = dtoOuts.stream().map(MessageDtoOut::getMid).collect(Collectors.toList());

            MessageDtoIn msg = new MessageDtoIn();
            msg.setMidList(mids);
            msg.setReceiverUid(in.getToUid());
            messageService.readMessages(msg);
        }

        return RespUtil.OK(MsgCode.MSG_SUCCESS);
    }

    /**
     * 删除所有消息
     *
     * @param json
     * @return
     */
    @RequestMapping(value = "deleteAllMessage", method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> deleteAllMessage(@RequestBody JSONObject json) {
        Assert.hasKeyAndValue(json, "toUid", "接受人不能为空");

        MessageDtoIn in = BeanConvertUtil.covertBean(json, MessageDtoIn.class);
        // 查出所有消息 并标记为删除
        Resp<List<MessageDtoOut>> resp = messageService.listMessages(in);
        if (resp.hasData()) {
            List<MessageDtoOut> dtoOuts = resp.getData();
            List<Long> mids = dtoOuts.stream().map(MessageDtoOut::getMid).collect(Collectors.toList());

            MessageDtoIn msg = new MessageDtoIn();
            msg.setMidList(mids);
            msg.setReceiverUid(in.getToUid());
            messageService.deleteAllMessage(msg);
        }

        return RespUtil.OK(MsgCode.MSG_SUCCESS);
    }

    /**
     * 解除标记消息已读
     *
     * @param json
     * @return
     */
    @RequestMapping(value = "unreadMessage", method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> unreadMessage(@RequestBody JSONObject json) {
        Assert.hasKeyAndValue(json, "receiverUid", "接受人不能为空");
        Assert.hasKeyAndValue(json, "midList", "消息ID不能为空");

        MessageDtoIn in = BeanConvertUtil.covertBean(json, MessageDtoIn.class);
        Resp<Void> resp = messageService.unreadMessages(in);
        return RespUtil.OK(resp);
    }

}
